package cn.tedu.sp06.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.web.util.JsonResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@Slf4j
public class RibbonController {

    @Autowired
    private RestTemplate restTemplate;

    //获取商品列表
    public JsonResult<List<Item>> getItemsFB(String orderId){
        return JsonResult.err().msg("获取商品列表失败");
    }

    //减少商品库存
    public JsonResult<?> decreaseNumberFB(List<Item> items){
        return JsonResult.err().msg("减少商品库存失败");
    }


    public JsonResult<User> getUserFB(Integer userId){
        return JsonResult.err().msg("获取用户失败");
    }

    public JsonResult<?> addUserFB(Integer userId,Integer score){
        return JsonResult.err().msg("增加积分失败");
    }


    public JsonResult<Order> getOrderFB(String orderId){
        return JsonResult.err().msg("获取订单失败");
    }

    public JsonResult<?> addOrderFB(String orderId){
        return JsonResult.err().msg("添加订单失败");
    }

    @HystrixCommand(fallbackMethod = "getItemsFB")
    @GetMapping("/item-service/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        //调用远程的商品服务， 获取商品列表  http://localhost:8001/{orderId}
        //{1} - RestTemplate 定义的占位符格式
        //第三个参数是对占位符进行填充的数据
       // eureka注册表：
        //item-service --- localhost:8001,localhost:8002

        return restTemplate.getForObject("http://item-service/{1}", JsonResult.class, orderId);
    }

    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    @GetMapping("/item-service/decreaseNumber")
    public JsonResult<?> decreaseNumber(List<Item> items){
        //调用远程服务路劲： http://localhost:8001/decreaseNumber
        // items  提交的协议提数据， 会把集合先转化为JSON
        return restTemplate.postForObject("http://item-service/decreaseNumber", items, JsonResult.class);
    }

    @HystrixCommand(fallbackMethod = "getUserFB")
    @GetMapping("/user-service/{userId}")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return restTemplate.getForObject("http://user-servicer/{1}", JsonResult.class, userId);
    }

    @HystrixCommand(fallbackMethod = "addUserFB")
    @GetMapping("/user-service/{userId}/score")  //  ?score=1000
    public JsonResult<?> addUser(@PathVariable Integer userId,Integer score){
        return restTemplate.getForObject("http://user-servicer/{1}/score?score={2}", JsonResult.class, userId);
    }

    @HystrixCommand(fallbackMethod = "getOrderFB")
    @GetMapping("/order-service/{orderId}")
    public JsonResult<Order> getOrder(String orderId){
        return restTemplate.getForObject("http://order-service/{1}",JsonResult.class, orderId);
    }

    @HystrixCommand(fallbackMethod = "addOrderFB")
    @GetMapping("/order-service/")
    public JsonResult<?> addOrder(String orderId){
        return restTemplate.getForObject("http://order-service/",JsonResult.class);
    }

}
